package com.dunqi.gpm.shunqing.develop.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.develop.adapter.EarlyWarningAdapter;
import com.dunqi.gpm.shunqing.develop.bean.EarlyWarning;
import com.dunqi.gpm.shunqing.home.activity.ProjectDetailActivity;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.fragment.HomeFragment;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/12/10.
 */

public class EarlyWarningFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArrayList<EarlyWarning> data = new ArrayList<>();
    private EarlyWarningAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectAlarmList(UserUtil.getUDA(), page)
                .compose(RxSchedulers.<Page<EarlyWarning>>io_main())
                .subscribe(new Consumer<Page<EarlyWarning>>() {
                    @Override
                    public void accept(Page<EarlyWarning> pages) throws Exception {
                        ArrayList<EarlyWarning> list = pages.getList();
                        SwipeUtil.loadCompleted(swipeRefresh);

                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }

                        page++;

                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        adapter.loadMoreFail();
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new EarlyWarningAdapter(data);
        recycler.setAdapter(adapter);

        swipeRefresh.setOnRefreshListener(this);

        adapter.setEmptyView(new EmptyView(context));
        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_early_warning;
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EarlyWarning warning = data.get(position);
        HomeFragment.homeMenuId = Constance.MENU_EARLY_WARNING;

        Project project = new Project();
        project.setProjectId(warning.getProjectId());
        ProjectDetailActivity.start(context, project, 0);
    }
}
